package com.youi.service.impl;

import com.youi.common.SessionKey;
import com.youi.dao.GroupleadserbankVoDao;
import com.youi.entity.Sysuser;
import com.youi.entity.Userorderexcel;
import com.youi.entity.vo.GroupleadserbankVo;
import com.youi.manage.HttpSessionManage;
import com.youi.service.GroupleadserbankVoService;
import com.youi.service.UserorderexcelService;
import com.youi.third.ali.oss.OSSUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.SmsUtil;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GroupleadserbankVoServiceImpl implements GroupleadserbankVoService {

	@Resource
	private GroupleadserbankVoDao groupleadserbankVoDao;
	
	@Autowired
	private UserorderexcelService userorderexcelService;
	@Override
	public List<GroupleadserbankVo> getGroupleadserbankVoList(Map<String,Object> param) {
		// TODO Auto-generated method stub
		List<GroupleadserbankVo> groupleadserbankVoList = groupleadserbankVoDao.getGroupleadserbankVoList(param);
		groupleadserbankVoList.stream().forEach(leader -> {
			leader.setStatusTxt(ObjectUtil.getCheckStaus(leader.getStatus()));
		});
		return groupleadserbankVoList;
	}


	@Override
	public Integer getGroupleadserbankVoCount(Integer status, String name, String leaderStr, String store,
			String start, String end, String auditStart, String auditEnd) {
		// TODO Auto-generated method stub
		return groupleadserbankVoDao.getGroupleadserbankVoCount(status, name, leaderStr, store, start, end, auditStart, auditEnd);
	}


	@Override
	public GroupleadserbankVo getGroupleadserbankVo(Long id) {
		// TODO Auto-generated method stub
		return groupleadserbankVoDao.getGroupleadserbankVo(id);
	}


	@Override
	public void toExcelGroupleaderbank(HttpServletRequest request, List<GroupleadserbankVo> groupleadserbankVoList) {
		// TODO Auto-generated method stub		
		try {
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			String fileName="groupleaderbank"+new Date().getTime()+""+SmsUtil.getRandom(3)+".xls";//文件名				
			String fileUrl=FileUploadUtil.getUploadUrl(request, "filedownload"+File.separator+fileName);//生成路径
			WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
			WritableSheet sheet1=workbook.createSheet("Sheet1", 0);
			WritableFont font=new WritableFont(WritableFont.createFont("微软雅黑"),10);
			WritableCellFormat format=new WritableCellFormat();
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			Label label=new Label(0, 0, "申请人", format);
			sheet1.addCell(label);
			label=new Label(1, 0, "身份证号", format);
			sheet1.addCell(label);
			label=new Label(2, 0, "微信ID", format);
			sheet1.addCell(label);
			label=new Label(3, 0, "手机号码", format);
			sheet1.addCell(label);
			label=new Label(4, 0, "认证门店编码", format);
			sheet1.addCell(label);
			label=new Label(5, 0, "认证门店", format);
			sheet1.addCell(label);
			label=new Label(6, 0, "所属银行", format);
			sheet1.addCell(label);
			label=new Label(7, 0, "开户省", format);
			sheet1.addCell(label);
			label=new Label(8, 0, "开户市", format);
			sheet1.addCell(label);
			label=new Label(9, 0, "状态", format);
			sheet1.addCell(label);
			GroupleadserbankVo groupleadserbankVo=null;
			for (int i = 0,j=1; i < groupleadserbankVoList.size(); i++,j++) {
				groupleadserbankVo=groupleadserbankVoList.get(i);
				ObjectUtil.ToChangeNullToEmpty(groupleadserbankVo);
				label=new Label(0, j, groupleadserbankVo.getName(), format);
				sheet1.addCell(label);
				label=new Label(1, j, groupleadserbankVo.getIdCard(), format);
				sheet1.addCell(label);
				label=new Label(2, j, groupleadserbankVo.getWechatName(), format);
				sheet1.addCell(label);
				label=new Label(3, j, groupleadserbankVo.getPhone(), format);
				sheet1.addCell(label);
				label=new Label(4, j, groupleadserbankVo.getAuthStoreCode(), format);
				sheet1.addCell(label);
				label=new Label(5, j, groupleadserbankVo.getAuthStore(), format);
				sheet1.addCell(label);
				label=new Label(6, j, groupleadserbankVo.getBank(), format);
				sheet1.addCell(label);
				label=new Label(7, j, groupleadserbankVo.getProvince(), format);
				sheet1.addCell(label);
				label=new Label(8, j, groupleadserbankVo.getCity(), format);
				sheet1.addCell(label);
				label=new Label(9, j, ObjectUtil.getCheckStaus(groupleadserbankVo.getStatus()), format);
				sheet1.addCell(label);
			}
			workbook.write();
			workbook.close();
			InputStream inStream=new FileInputStream(fileUrl);
			byte[]	data = ObjectUtil.readInputStream(inStream);
			String dowmupload=OSSUtil.upload(data, ".xls");
			if (dowmupload!=null&&dowmupload.length()>0) {
				userorderexcelService.saveOrUpdate(new Userorderexcel(sysuser.getId(), 6, dowmupload));
			}
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

}
